package com.swt.draw.controller;

import com.swt.draw.entity.Yjk;
import com.swt.draw.service.YjkService;
import com.swt.draw.vo.BaseResponse;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author Administrator
 * @Date 2019/10/27 0027 10:52
 * @Version 1.0
 **/
@RestController
@RequestMapping("/yjk")
public class YjkController {

    @Resource
    private YjkService yjkService;

    /**
     * @Author ZPF
     * @Description 根据原件代码查询
     * @Date 10:58 2019/10/27 0027
     * @Param [yjk]
     * @return com.swt.draw.vo.BaseResponse<java.util.List<com.swt.draw.entity.Yjk>>
     **/
    @RequestMapping("/getByDm")
    public BaseResponse<List<Yjk>> getYjk(Yjk yjk){
        BaseResponse<List<Yjk>> response = new BaseResponse();
        List<Yjk> yjks = yjkService.getYjkByDm(yjk.getDm());
        response.setData(yjks);
        return response;
    }

    /**
     * @Author ZPF
     * @Description 添加图元
     * @Date 11:07 2019/10/27 0027
     * @Param [yjk, file]
     * @return com.swt.draw.vo.BaseResponse
     **/
    @RequestMapping("/add")
    public BaseResponse addYjk(Yjk yjk, MultipartFile file) throws IOException {
        BaseResponse response = new BaseResponse();
        if(file == null || file.isEmpty()){
            return response;
        }
        yjk.setPic(file.getBytes());
        yjk.setId(new BigDecimal(yjkService.getMaxId() + 1));
        Integer row = yjkService.addYjkSelective(yjk);
        if(row > 0){
            response.setCode(1001);
            response.setDesc("success");
        }
        return response;
    }

    /**
     * @Author ZPF
     * @Description 更新元件
     * @Date 20:09 2019/10/27 0027
     * @Param [yjk, file]
     * @return com.swt.draw.vo.BaseResponse
     **/
    @RequestMapping("/update")
    public BaseResponse updateYjk(Yjk yjk, MultipartFile file) throws IOException {
        BaseResponse response = new BaseResponse();
        if(file == null || !file.isEmpty()){
            yjk.setPic(file.getBytes());
        }
        Integer rows = yjkService.updateYjkSelective(yjk);
        if(rows > 0){
            response.setCode(1001);
            response.setDesc("success");
        }
        return response;
    }

    /**
     * @Author ZPF
     * @Description 删除图元
     * @Date 22:36 2019/10/27 0027
     * @Param [dm]
     * @return com.swt.draw.vo.BaseResponse
     **/
    @RequestMapping("/delete")
    public BaseResponse deleteYjk(String[] dms){
        BaseResponse response = new BaseResponse();
        if(ArrayUtils.isEmpty(dms)){
            return response;
        }
        Integer rows = yjkService.deleteYjkByDm(dms);
        if(rows > 0){
            response.setCode(1001);
            response.setDesc("success");
        }
        return response;
    }
}
